package com.tian.print.http;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.tian.print.R;
import com.tian.print.app.RepositoryManagerApp;
import com.tian.print.utils.Utils;


/**
 * Description: 加载框
 * Author：skyTian
 * Time:2017/11/13 10:51
 * CopyRight:com,sky
 */
public class LoadingDialog extends DialogFragment {

    /**
     * 根布局
     */
    private View rootView;


    public static LoadingDialog newInstance(Bundle bundle) {
        LoadingDialog loadingDialog = new LoadingDialog();

        if (bundle != null)
            loadingDialog.setArguments(bundle);

        return loadingDialog;
    }


//    @Override
//    public Dialog onCreateDialog(Bundle savedInstanceState) {
////        if (getDialog() != null) {
////            getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
////            getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND);
////        }
//
//        rootView = LayoutInflater.from(getActivity()).inflate(R.layout.progress_layout, null);
//
//        return new AlertDialog.Builder(getActivity()).setView(rootView)
//                .create();
//    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (getDialog() != null) {
            getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
            getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }

        rootView = inflater.inflate(R.layout.progress_layout, container, false);
        initView();
        return rootView;
    }


    private void initView() {
    }

    public void show(Context context) {
        show(((Activity) context).getFragmentManager(), "");
    }

    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow();
        WindowManager.LayoutParams winParams = window.getAttributes();
        winParams.dimAmount = 0.0f;

        window.setAttributes(winParams);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getDialog() != null) {
//            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#80FFFFFF")));

            if (getActivity() != null)
                getDialog().getWindow().setLayout((int) (Utils.getWindowWidth(RepositoryManagerApp.getAppContext())), (int) (Utils.getWindowHeigh(RepositoryManagerApp.getAppContext())));
        }
    }

}
